using Microsoft.SharePoint.Utilities;
using System.IO;

namespace NikCodes.SharePoint.ClientSide
{
    /// <summary>
    /// Contains useful static methods and attributes
    /// </summary>
    public static class Utilities
    {
        /// <summary>
        /// The prefix code for all SPCS properties (property bag)
        /// </summary>
        public static string PrefixCode = "SPCS";
        /// <summary>
        /// The full path for jquery UI Themes
        /// </summary>
        public static string JqueryUIThemesPath = SPUtility.GetGenericSetupPath("") + @"\Template\Layouts\ClientSide\jqueryui\themes";

        /// <summary>
        /// Returns if a property (PropertyBag) is set to true or not
        /// </summary>
        /// <param name="props">The property bag to look in</param>
        /// <param name="key">The key to check</param>
        /// <returns>True if the property is set and true. False if not</returns>
        public static bool IsPropertySet(SPPropertyBag props, string key)
        {
            return props.ContainsKey(Utilities.PrefixCode + key) && bool.Parse(props[Utilities.PrefixCode + key]);
        }

        /// <summary>
        /// Returns a specified property from the property bag
        /// </summary>
        /// <param name="props">The propertybag</param>
        /// <param name="key">The key to search for</param>
        /// <returns>The value of the propertie</returns>
        public static string GetProperty(SPPropertyBag props, string key)
        {
            // We try to get the property and return
            try
            {
                return props[Utilities.PrefixCode + key];
            }
            catch
            {
                return "";
            }
        }

        /// <summary>
        /// Returns the list of available themes for jQueryUI
        /// </summary>
        /// <returns>A List of themes' names in an array</returns>
        public static string[] GetAllJQueryUIThemes()
        {
            string[] themesFullPath = Directory.GetDirectories(JqueryUIThemesPath);
            string [] themesNames = new string[themesFullPath.Length];
            int i = 0;
            foreach (string themeFullPath in themesFullPath)
            {
                themesNames[i] = Path.GetFileName(themeFullPath);
                i++;
            }
            return themesNames;
        }
    }
}
